package com.world.common.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.world.common.domain.PageBaseRequest;
import com.world.common.domain.PageResult;

import java.util.List;

/**
 * 基础Controller
 *
 * @author 邓驴
 * @date 2023/5/13 16:36
 **/
public class BaseController<T> {

    public Page<T> startPage(PageBaseRequest request) {
        long current = request.getCurrent() == null ? 1L : request.getCurrent();
        long size = request.getSize() == null ? 10L : request.getSize();
        return new Page<>(current, size);
    }


    /**
     * 转换分页列表返回值
     *
     * @param page  mybatisPlus分页结果
     * @param clazz 转换的对象
     * @return {@link PageResult}
     * @author 邓驴
     * @date 2023/5/13 17:05
     **/
    public <K> PageResult<K> getPageResult(IPage<T> page, Class<K> clazz) {
        PageResult<K> pageResult = new PageResult<>();
        List<K> vos = BeanUtil.copyToList(page.getRecords(), clazz);
        pageResult.setRecords(vos);
        pageResult.setCurrent(page.getCurrent());
        pageResult.setSize(page.getSize());
        pageResult.setTotal(page.getTotal());
        return pageResult;
    }

    /**
     * 转换分页列表返回值
     *
     * @param page mybatisPlus分页结果
     * @return {@link PageResult<T>}
     * @author WangYX
     * @date 2023/09/04 16:57
     */
    public <K> PageResult<K> getPageResult(IPage<K> page) {
        PageResult<K> pageResult = new PageResult<>();
        pageResult.setRecords(page.getRecords());
        pageResult.setCurrent(page.getCurrent());
        pageResult.setSize(page.getSize());
        pageResult.setTotal(page.getTotal());
        return pageResult;
    }

}

